package zj.com.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.xutils.DbManager;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.FragmentAdapter;
import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.fragments.DshDetailEditFragment;
import zj.com.fragments.DshDetailSpFragment;
import zj.com.fragments.DzgDetailEditFragment;
import zj.com.fragments.DzgDetailSpFragment;
import zj.com.model.AuditLog;
import zj.com.model.DzgInspectItemDB;
import zj.com.model.DzgInspectTask;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/12.
 */
public class DshDetailEditActivity extends FragmentActivity implements View.OnClickListener{
    public TextView title_name;
    public ImageView left_iv;
    public TextView right_tv;

    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    private int screenWidth;
    private ViewPager mPageVp;
    /**
     * Tab显示内容TextView
     */
    private TextView mjcxTv, mjcdTv;
    /**
     * Tab的那个引导线
     */
    private ImageView mTabLineIv;

    private  Fragment  dshEdit,dzgSp;
    private int currentIndex;

    public static List<AuditLog> auditList;
    public LinearLayout id_tab_friend_ll,id_tab_chat_ll;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dsh_detail_edit);
        initView();

        init();
        initTabLineWidth();

    }

    public void initView(){

        id_tab_chat_ll = (LinearLayout) findViewById(R.id.id_tab_chat_ll);
        id_tab_chat_ll.setOnClickListener(this);
        id_tab_friend_ll = (LinearLayout) findViewById(R.id.id_tab_friend_ll);
        id_tab_friend_ll.setOnClickListener(this);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("详情");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setOnClickListener(this);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setText("通过");
        right_tv.setVisibility(View.GONE);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);

        mPageVp = (ViewPager) findViewById(R.id.id_page_vp);
        mjcxTv = (TextView) findViewById(R.id.id_jcx_tv);
        mjcdTv = (TextView) findViewById(R.id.id_jcd_tv);
        mTabLineIv = (ImageView) findViewById(R.id.id_tab_line_iv);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        dshEdit.onActivityResult(requestCode,resultCode,data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    public static DzgInspectTask task;
    private void init() {
        dshEdit = new DshDetailEditFragment();
        dzgSp = new DshDetailSpFragment();
        mFragmentList.add(dshEdit);
        mFragmentList.add(dzgSp);


        try {
            task = (DzgInspectTask) getIntent().getSerializableExtra("task");
            DbManager dbManager = DBUtil.getInstance(this).getDBManager();
            DzgInspectItemDB inspectDB =  dbManager.selector(DzgInspectItemDB.class).where("inspectcontentid", "=", task.getInspectTaskID()).findFirst();
            if(inspectDB == null){
                DzgInspectItemDB inspect = new DzgInspectItemDB();
                inspect.setInspectContentID(task.getInspectTaskID());
                ZjApplication.dzgInspectItemDB = inspect;
            }else{
                ZjApplication.dzgInspectItemDB =   inspectDB;
            }


        }catch(Exception e){

        }

        mFragmentAdapter = new FragmentAdapter(
                this.getSupportFragmentManager(), mFragmentList);
        mPageVp.setAdapter(mFragmentAdapter);
        mPageVp.setCurrentItem(0);

        mPageVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }

            /**
             * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
             * offsetPixels:当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float offset,
                                       int offsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();

                Log.e("wxf", "postion=" + position + "    offset=" + offset + " =====currentIndex=" + currentIndex);

                if (currentIndex == 0 && position == 0)// 0->1
                {
//                    lp.leftMargin = (int) (offset * (screenWidth * 1.0) + currentIndex
//                            * (screenWidth / 2));
                    lp.leftMargin =
                            0;
                    Log.e("wxf","0  0");


                }else if(currentIndex ==1 && position ==0){
                    Log.e("wxf","1  0");
                    lp.leftMargin =
                            screenWidth / 2;
                }else if(currentIndex ==1 && position ==1){

                    Log.e("wxf","1  1");
                    lp.leftMargin =
                            screenWidth / 2;
                }
                // mTabLineIv.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                resetTextView();
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();
                switch (position) {
                    case 0:
                        mjcdTv.setTextColor(getResources().getColor(R.color.color_text_p));

                        lp.leftMargin =0;
                        break;
                    case 1:


                        lp.leftMargin =
                                screenWidth / 2;
                        mjcxTv.setTextColor(getResources().getColor(R.color.color_text_p));
                        ;
                        break;

                }
                mTabLineIv.setLayoutParams(lp);
                currentIndex = position;
            }
        });

    }

    /**
     * 设置滑动条的宽度为屏幕的1/3(根据Tab的个数而定)
     */
    private void initTabLineWidth() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                .getLayoutParams();
        lp.width = screenWidth / 2;
        mTabLineIv.setLayoutParams(lp);
    }

    /**
     * 重置颜色
     */
    private void resetTextView() {
        mjcxTv.setTextColor(Color.parseColor("#828282"));
        mjcdTv.setTextColor(Color.parseColor("#828282"));
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.id_tab_friend_ll:
                mPageVp.setCurrentItem(1);
                break;
            case R.id.id_tab_chat_ll:
                mPageVp.setCurrentItem(0);

                break;
        }
    }
}
